(ns lensfield.defaults
   (:require [clojure.contrib.str-utils2 :as su2]
             [lensfield.file-utils :as futils]))

; LENSFIELD DISTRIBUTION DEFAULTS

(def lf-home (futils/unix-filepath (System/getenv "LENSFIELD_HOME")))
(def lf-build-home (str lf-home "/lensfield-build"))

(def lf-webapp-home (str lf-home "/lensfield-webapp"))
(def lf-webapp-src (str lf-webapp-home "/src"))
(def lf-webapp-pom (str lf-webapp-home "/pom.xml"))

(def lf-props-name "lensfield.properties")
(def lf-build-state-name "build-state.clj")
(def lf-props-template (str lf-build-home "/src/main/resources/" lf-props-name))


; USER PROJECT DEFAULTS

(def *properties-defaults*
     {:username ""
      :build-file "build.clj"
      :data-dir "data"
      :project-name "Lensfield project"
      :backup-server-url ""})
      
(def lf-project-ns 'lensfield.project)
(def project-props-file (str "./" lf-props-name))
(def project-build-state-file (str "./" lf-build-state-name))

; default files for web application
(def webapp-fn-name "create-webapp")
(def project-webapp-dir "./webapp")
(def project-webapp-clj-dir (str project-webapp-dir "/src/main/clj/lensfield/webapp"))
(def project-webapp-pom (str project-webapp-dir "/pom.xml"))
(def project-mvn-webapp-dir (str project-webapp-dir "/src/main/webapp"))
(def project-triplestore-dir (str project-mvn-webapp-dir "/triple_store"))
(def project-atom-feed (str project-mvn-webapp-dir "/atom/feed.xml"))
;(def project-webapp-props-file (str project-mvn-webapp-dir "/" lf-props-name))
(def project-webapp-config-file (str project-webapp-clj-dir "/config.clj"))
(def project-web-xml (str project-mvn-webapp-dir "/WEB-INF/web.xml"))

; default project Mercurial files
(def dot-hg-dir "./.hg")
(def hgrc-file (str dot-hg-dir "/hgrc"))

; default Lensfield configuration files
(def dot-lf-dir "./.lf")
(def dot-lf-backed-up-file (str dot-lf-dir "/backed-up"))

(defn get-props
   "Loads a properties file (which contains a clojure map).  If no file is
   provided then a file from a default will be loaded."
   ([] 
      (get-props project-props-file))
   ([props-file]
      (merge *properties-defaults* 
         (filter #(not (su2/blank? (val %1))) 
                     (when (futils/file-exists project-props-file)
                        (load-file props-file))))))
                        
(defn create-dot-lf-file
   [fl]
   (when (not (futils/file-exists dot-lf-dir))
      (futils/mkdir dot-lf-dir))
   (futils/create-file fl))

(defn mvn-war-path
   "The location that Maven creates a webapp to after 
   'mvn package' is run."
   [nme]
   (str project-webapp-dir "/target/" nme ".war"))
   
(defn project-war-path
   "The location that a project's webapp will be stored."
   [nme]
   (str "./" nme ".war"))
   
(defn backup-endpoint
   "Given the URL of a Lensfield repository server, will return
   the endpoint that a backup should be POSTed to."
   [repo-url]
   (str repo-url "create.py"))